package com.qiaoyanqing.ximalaya.discovermodule.discover_adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.qiaoyanqing.ximalaya.R;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.DiscoverRankListEntity;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;

import java.util.List;

/**
 * Created by 佳男 on 2016/12/4.
 */

public class DiscoverRankAdapter extends BaseAdapter {
    private List<DiscoverRankListEntity> list;
    private Context context;
    private LayoutInflater inflater;

    public DiscoverRankAdapter(Context context, List<DiscoverRankListEntity> list) {
        this.context = context;
        this.list = list;
        inflater=LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final ViewHolder holder;
        if(view==null){
            view=inflater.inflate(R.layout.rank_item,viewGroup,false);
            holder=new ViewHolder(view);
            view.setTag(holder);


        }else{
            holder= (ViewHolder) view.getTag();
        }
        holder.rank_title.setText(list.get(i).getTitle());
        holder.rank_top1.setText("1 "+list.get(i).getFirstKResults().get(0).getTitle());
        holder.rank_top2.setText("2 "+list.get(i).getFirstKResults().get(1).getTitle());
        String url=list.get(i).getCoverPath();
        Transformation transformation=new Transformation() {
            @Override
            public Bitmap transform(Bitmap source) {
                int width=holder.rank_image.getWidth();
                if(source.getWidth()==0){
                    return source;
                }else if (source.getWidth()<width) {
                    return source;
                }else{
                    double aspectRatio=(double)source.getHeight()/source.getWidth();
                    int height=(int)(width*aspectRatio);
                    if(width!=0&&height!=0){
                        Bitmap result=Bitmap.createScaledBitmap(source,width,height,false);
                        if(result!=source){
                            source.recycle();
                        }
                        return result;
                    }else{
                        return source;
                    }
                }
            }

            @Override
            public String key() {
                return "transformation"+"desiredWidth";
            }
        };

        Picasso.with(context)
                .load(url)
                .transform(transformation)
                .into(holder.rank_image);
        return view;
    }

    private static class ViewHolder{
        ImageView rank_image;
        TextView rank_title;
        TextView rank_top1;
        TextView rank_top2;
        public ViewHolder(View view){
            rank_image= ((ImageView) view.findViewById(R.id.rank_image));
            rank_title=((TextView)view.findViewById(R.id.rank_title));
            rank_top1=((TextView)view.findViewById(R.id.rank_top1));
            rank_top2=((TextView)view.findViewById(R.id.rank_top2));
        }

    }
}
